package pers.yd.springbootmvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.yd.springbootmvc.dao.MoviesDao;
import pers.yd.springbootmvc.dto.Movies;

import java.util.Arrays;
import java.util.List;

/**
 * @author yudi
 */
@RestController
@RequestMapping("/movies")
public class MoviesController {

    @Autowired
    private MoviesDao moviesDao;

    @GetMapping
    public List<Movies> getPageDataByTitle(@RequestParam String title,
                                    @RequestParam(defaultValue = "0")Integer page,
                                    @RequestParam(defaultValue = "10")Integer size){
        Pageable pageable = PageRequest.of(page, size);
        Page<Movies> moviesPage = moviesDao.findByTitle(title,pageable);
        return moviesPage.getContent();
    }

    @GetMapping("/multi-fields")
    public List<Movies> getPageDataByTitleAndGenre(String title, String genre,
                                                   @RequestParam(defaultValue = "0")Integer page,
                                                   @RequestParam(defaultValue = "10")Integer size){
        Pageable pageable = PageRequest.of(page, size);
        Page<Movies> moviesPage = moviesDao.findByTitleAndGenre(title, genre, pageable);
        return moviesPage.getContent();
    }


    @GetMapping("/in")
    public List<Movies> getPageDataInTitle(String[] titles,
                                           @RequestParam(defaultValue = "0")Integer page,
                                           @RequestParam(defaultValue = "10")Integer size){
        Pageable pageable = PageRequest.of(page, size);
        Page<Movies> moviesPage = moviesDao.findByTitleIn(Arrays.asList(titles), pageable);
        return moviesPage.getContent();
    }

    @GetMapping("/be")
    public List<Movies> getPageDataTitleAndYearBetween(String title, Long start, Long end,
                                                       @RequestParam(defaultValue = "0")Integer page,
                                                       @RequestParam(defaultValue = "10")Integer size){
        Pageable pageable = PageRequest.of(page, size);
        Page<Movies> moviesPage = moviesDao.findByTitleAndYearBetween(title, start, end, pageable);
        return moviesPage.getContent();
    }


    @GetMapping("/self")
    public List<Movies> get(String title, String genre,
                            @RequestParam(defaultValue = "0")Integer page,
                            @RequestParam(defaultValue = "10")Integer size){
        return moviesDao.get(title, genre);
    }
}
